package com.guigui.gulimall.product.controller;

import com.guigui.common.utils.PageUtils;
import com.guigui.common.utils.R;
import com.guigui.gulimall.product.entity.ProductAttrValueEntity;
import com.guigui.gulimall.product.service.AttrService;
import com.guigui.gulimall.product.service.ProductAttrValueService;
import com.guigui.gulimall.product.vo.AttrResponseVo;
import com.guigui.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 商品属性
 *
 * @author xin
 * @email 806748502@qq.com
 * @date 2021-07-23 15:09:03
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {

    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    /**
     * 通过spuId查询商品属性
     * @param spuId spu_id
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R listBySpu(@PathVariable("spuId") Long spuId) {
        List<ProductAttrValueEntity> list = productAttrValueService.listBySpu(spuId);
        return R.ok().put("data", list);
    }

    /**
     * 列表
     */
    @RequestMapping("/list}")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 查询基本属性或销售属性 by catelogId
     */
    @GetMapping("/{attrType}/list/{catelogId}")
    public R attrBaseList(@RequestParam Map<String, Object> params,
                          @PathVariable("catelogId") Long catelogId,
                          @PathVariable("attrType") String attrType) {

        PageUtils page = attrService.queryAttrBasePage(params, catelogId, attrType);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){

        AttrResponseVo responseVo = attrService.getAttrRespVo(attrId);

        return R.ok().put("attr", responseVo);
    }

    /**
     * 保存
     */
    @Transactional
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attrVo){

        attrService.saveAttr(attrVo);
//        attrService.save(attrEntity);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attrVo){
		attrService.updateAttr(attrVo);
        return R.ok();
    }

    /**
     * 修改spu商品属性
     */
    @PostMapping("/update/{spuId}")
    public R updateSpuAttr(@PathVariable("spuId") Long spuId,
                           @RequestBody List<ProductAttrValueEntity> attrs){
        productAttrValueService.updateSpuAttr(spuId, attrs);
        return R.ok();
    }


    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
